/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/statfs.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "fs/dirent_fs.h"
#include "fs/fs.h"
#include "internal.h"
#include "proc_file.h"
#include "los_process.h"
#include "los_process_pri.h"
#include "los_container_bundle.h"
#include "los_uts_container.h"
#include "proc_fs.h"
#include "los_vm_dump.h"

#define SYS_DIR_MODE (S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
#define PROCFS_FILE_MODE (S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO)
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array)    (sizeof(array) / sizeof(array[0]))
#endif

static char * g_procfsContainerFn[] = {
    "max_mnt_container",
    "max_pid_container",
    "max_user_container",
    "max_net_container",
    "max_uts_container",
    "max_time_container",
    "max_ipc_container",
};

struct ContainerConfig g_containerConfig;

void OsContainerConfigInit(UINT32 defalutValue)
{
    UINT32* pData = (UINT32*)&g_containerConfig;
    for(int i = 0; i < sizeof(g_containerConfig)/sizeof(UINT32); i++) {
        pData[i] = defalutValue;
    }
}

UINT32 LookupContainerFnIndex(char * data)
{
    for (int i = 0; i < ARRAY_SIZE(g_procfsContainerFn); i++) {
        if (strncmp(data, g_procfsContainerFn[i], strlen(g_procfsContainerFn[i]))) {
             return i;
        }
    }
    return -LOS_NOK;
}

static int FsMaxContainerRead(struct SeqBuf *seqBuf, void *arg)
{
    UINT32 index = LookupContainerFnIndex((char*)arg);
    if (index >= ARRAY_SIZE(g_procfsContainerFn)) {
        return LOS_NOK;
    }
    UINT32 *pData = (UINT32*)&g_containerConfig;
    (void)LosBufPrintf(seqBuf, "%u\n", pData[index]);
    return 0;
}

static ssize_t FsMaxContainerWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
{
    if (buf == NULL) {
        return 0;
    }

    INT32 value = atoi(buf);
    if (value > LOSCFG_BASE_CORE_PROCESS_LIMIT || value < 1) {
        PRINTK("invalid value: %d\n", value);
        return LOS_NOK;
    }

    UINT32 index = LookupContainerFnIndex((char*)pf->pPDE->data);
    if (index >= ARRAY_SIZE(g_procfsContainerFn)) {
        return LOS_NOK;
    }
    UINT32 *pData = (UINT32*)&g_containerConfig;
    pData[index] = value;
    return 0;
}

static struct ProcFileOperations FS_MAX_CONTAINER_PROC_FOPS = {
    .read = FsMaxContainerRead,
    .write = FsMaxContainerWrite,
};

struct ProcDirEntry * ProcfsCreateEntry(char *name,struct ProcDirEntry *entry)
{
    struct ProcDirEntry *pde = CreateProcEntry(name, SYS_DIR_MODE, entry);
    if (pde == NULL) {
        PRINT_ERR("create user error!\n");
        return NULL;
    }

    return pde;
}

void ProcFsSysInit()
{
    struct ProcDirEntry *pde = ProcfsCreateEntry("sys", NULL);
    if (pde == NULL) {
        PRINT_ERR("create /proc/sys error!\n");
        return;
    }

    struct ProcDirEntry *pSubDe = ProcfsCreateEntry("user", pde);
    if (pSubDe == NULL) {
        PRINT_ERR("create /proc/sys/user error!\n");
        return;
    }

    struct ProcDirEntry *pn = NULL;
    for (int i = 0; i < ARRAY_SIZE(g_procfsContainerFn); i++) {
        pn = ProcCreateData(g_procfsContainerFn[i], PROCFS_FILE_MODE, pSubDe, pSubDe->procFileOps, g_procfsContainerFn[i]);
        if (pn == NULL) {
            PRINT_ERR("Create ProcDirEntry failed\n");
            return;
        }
        pn->procFileOps = &FS_MAX_CONTAINER_PROC_FOPS;
    }
}
